package com.jhws.business.property.controller;

import com.jhws.common.base.bo.JsonRespData;
import com.jhws.business.property.bo.PropertyFeeFormula;
import com.jhws.business.property.bo.PropertyFeeInfo;
import com.jhws.business.property.service.PropertyFeeFormulaService;
import com.jhws.business.property.service.PropertyFeeService;
import com.jhws.util.DateUtil;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/property/fee")
public class PropertyFeeController {
    @Resource
    private PropertyFeeFormulaService propertyFeeFormulaService;
    @Resource
    private PropertyFeeService propertyFeeService;

    /**
     * 根据业主ID查询物业缴费信息
     */
    @RequestMapping("queryPropertyFeeByOwnerCode.x")
    @ResponseBody
    public JsonRespData queryPropertyFeeByOwnerCode(String ownerCode, Integer isPay) throws ParseException {
        return new JsonRespData("", 1, propertyFeeService.queryPropertyFeeByOwnerCode(ownerCode, isPay));
    }

    /**
     * 批量导入信息,比如excel表
     *
     * @return
     */
    @RequestMapping(value = "saveBatchsPropertyFee.x")
    @ResponseBody
    public JsonRespData saveBatchPropertyFee(@RequestBody PropertyFeeInfo[] propertyFeeInfos) throws Exception {
        for (PropertyFeeInfo propertyFeeInfo : propertyFeeInfos) {
            propertyFeeService.savePropertyFee(propertyFeeInfo);
        }
        return new JsonRespData("", 1, "导入成功,数据总条目" + propertyFeeInfos.length);
    }

    /**
     * 导出数据
     *
     * @param commCode
     * @return
     * @throws Exception
     */
    @RequestMapping("getPropertyFeesForExport.x")
    @ResponseBody
    public JsonRespData getPropertyFeesForExport(String commCode) throws Exception {
        return new JsonRespData("", 1, propertyFeeService.getPropertyFeesForExport(commCode));
    }

    /**
     * 根据小区保存最新的账单
     */
    @RequestMapping("createPropertyFee.x")
    @ResponseBody
    public JsonRespData createPropertyFee(String commCode) throws Exception {
        propertyFeeService.savePropertyFeeByCommCode(commCode);
        return new JsonRespData("", 1, "");
    }

    /**
     * 分页查询物业缴费信息
     *
     * @param param Map
     * @return 物业缴费信息列表
     */
    @RequestMapping("queryPropertyFeePage.x")
    @ResponseBody
    public JsonRespData queryPropertyFeePage(@RequestParam Map param) throws Exception {
        return new JsonRespData("", 1, propertyFeeService.queryPropertyFeePage(param));
    }

    /**
     * 更新状态
     */
    @RequestMapping("resetState.x")
    @ResponseBody
    public JsonRespData resetState(String id, int isPay, String lastBillDate, String commCode) {

        if (StringUtils.isEmpty(lastBillDate)) {
            return new JsonRespData("", -1, "上次缴费账单日期必须选择！");
        }

        String where = "where is_enable='1' and comm_code='" + commCode + "'";
        List<PropertyFeeFormula> propertyFeeFormulas = propertyFeeFormulaService.queryPropertyFeeFormulaWhere(where);
        if (propertyFeeFormulas == null || propertyFeeFormulas.size() != 1) {
            return new JsonRespData("", -1, "公式错误！");
        }
        PropertyFeeFormula propertyFeeFormula = propertyFeeFormulas.get(0);
        int day = DateUtil.getDateByField(DateUtil.stringDate(lastBillDate, DateUtil.DATE_FORMAT), Calendar.DAY_OF_MONTH);
        if (propertyFeeFormula.getBillDay() != day) {
            return new JsonRespData("", -1, "你选择的日期不是账单日！");
        }
        propertyFeeService.saveState(id, isPay, lastBillDate);
        return new JsonRespData("", 1, "");
    }

    /**
     * 按小区催缴物业费
     */
    @RequestMapping("callPropertyFee.x")
    @ResponseBody
    public JsonRespData callPropertyFee(String commCode) throws Exception {
        propertyFeeService.callPropertyFee(commCode);
        return new JsonRespData("", 1, "");
    }
}
